/**
 * Created by LeungZ on 2016/8/8.
 */
import tmp from './form/pause.html'
import ctrl from './form/pause.ctrl.js'

class Node {
    constructor(nodeSrv, list, ctgNotification, $uiModal, $timeout, $scope, $stateParams) {
        "ngInject";
        this._nodeSrv = nodeSrv;
        this._notification = ctgNotification;
        this._uiModal = $uiModal;
        this._timeout = $timeout;
        this.timer = '';
        this.dbCode = $stateParams.kDbCode;

        this.page = {
            currentPage: 1,
            totalItems: 0
        };
        this.filter = {
            name: null,
            groupCode: null
        };

        if (list) {
            this.initTable(list);
        } else this.table = [];

        $scope.$on('$destroy', ()=> {
            this._timeout.cancel(this.timer);
        });

        $scope.$on('searchByGroup', (e, code) =>{
            this.filter.groupCode = code;
            this.search();
        })
    }

    initTable(data){
        this.resData = [];
        this.nodeIps = [];

        angular.forEach(data.nodes, (_node, index)=> {
            this.nodeIps.push(_node.nodeIp);
            this.setNodesResource(_node, index);
            _node.nodeStatus = _.findWhere(_node.conditions, {type: 'Ready'}).status == 'True';
        });

        this.table = data.nodes;
        this.page.totalItems = data.readyNodeNum + data.notReadyNodeNum;

        if(this.nodeIps.length)this.refreshNodesResource();
    }

    getList() {
        this._timeout.cancel(this.timer);
        this.table = null;
        this._nodeSrv.getNodeList({page: this.page.currentPage, code: this.dbCode}, this.filter).then(data=> {
            this.initTable(data);
        }).catch(()=> {
            this.table = [];
            this.page.totalItems = 0;
            this._notification.error('获取宿主机列表数据有误.');
        });
    }

    stopNode(elem) {
        this._uiModal.open({
            templateUrl: tmp,
            controller: ctrl,
            controllerAs: 'vm',
            windowClass: 'alert-dialog alert-dialog-ctg',
            resolve: {
                nodeIp: function () {
                    return elem.nodeIp;
                }
            }
        }).result.then(()=> {
                elem.unschedulable = 'loading';
                this._nodeSrv.stopNode({ip: elem.nodeIp, code: this.dbCode}).then(data=> {
                    elem.unschedulable = data.unschedulable;
                    this._notification.success('停用宿主机 ' + elem.nodeIp + ' 成功.');
                }).catch(data=> {
                    elem.unschedulable = false;
                    this._notification.error('停用宿主机 ' + elem.nodeIp + ' 失败.');
                })
            })
    }

    startNode(elem) {
        elem.unschedulable = 'loading';
        this._nodeSrv.startNode({ip: elem.nodeIp, code: this.dbCode}).then(data=> {
            elem.unschedulable = data.unschedulable;
            this._notification.success('启用宿主机 ' + elem.nodeIp + ' 成功.');
        }).catch(data=> {
            elem.unschedulable = true;
            this._notification.error('启用宿主机 ' + elem.nodeIp + ' 失败.');
        })
    }

    search() {
        this.page.currentPage = 1;
        this.getList();
    }

    refreshNodesResource() {
        this.timer = this._timeout(this.getNodesRes.bind(this), 10000);
    }

    getNodesRes() {
        this._nodeSrv.getNodesResource({ips: this.nodeIps.join(','), code: this.dbCode}).then(data=> {
            angular.forEach(data, (node, index)=> {
                this.table[index].nodeStatus = _.findWhere(node.conditions, {type: 'Ready'}).status == 'True';
                this.setNodesResource(node, index);
            });
            this.refreshNodesResource();
        }).catch(data=> {
            this._notification.error('获取宿主机资源数据有误.');
        });
    }

    setNodesResource(node, index) {
        this.resData[index] = {
            cpu: node.dynamicStatus.totalCpuUsage,
            usedMem: node.dynamicStatus.memoryUsage,
            totalMem: node.dynamicStatus.totalMemory
        };
    }

    groupActive(){
        this.btn_group_list = !this.btn_group_list;
    }
}

export default Node;